import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xreader/app/app_routes.dart';
import 'package:xreader/controller/book_lib_controller.dart';
import 'package:xreader/logs/log.dart';
import 'package:xreader/model/t_book_entity.dart';
import 'package:xreader/pages/widgets/search_box_widget.dart';

class BookLibPage extends StatefulWidget {
  const BookLibPage({Key? key}) : super(key: key);

  @override
  State<BookLibPage> createState() => _BookLibPageState();
}

class _BookLibPageState extends State<BookLibPage> with AutomaticKeepAliveClientMixin {
  late final SearchBoxWidget searchBoxWidget;

  @override
  void initState() {
    BookLibController bookLibController = Get.put(BookLibController());
    searchBoxWidget = SearchBoxWidget(
      okSearch: (text){
        Log.i("搜索===$text");
        bookLibController.searchText = text;
        bookLibController.searchData(1,false);
      },
    );
    Log.i("BookLibPage 初始化------》");
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      titleSpacing: 0,
      toolbarHeight: 44,
      backgroundColor: Colors.white,
      elevation: 0,
      //title: const SearchBoxGx(),
      title: searchBoxWidget,
    ),
    body: GetBuilder<BookLibController>(
      //init: BookLibController(),
      builder: (BookLibController bookLibController){
        return Column(
          children:[
            Expanded(
                child: ListView.builder(
                    controller: bookLibController.scrollController,
                    itemCount: bookLibController.datas.length + ( bookLibController.isLoading  ? 1 : 0),
                    itemBuilder: (context,index){
                      if (index < bookLibController.datas.length) {
                        TBookEntity info = bookLibController.datas[index];
                        String bauthor = info.bauthor ?? "";
                        String bchao = info.bchao ?? "";
                        int bcount = info.bcount ?? 0;
                        String abountTitle = "";
                        if(bchao.isNotEmpty){
                          abountTitle += "[${bchao}]";
                        }
                        if(bauthor.isNotEmpty){
                          abountTitle += " ${bauthor}";
                        }
                        Container con = Container(
                          decoration: BoxDecoration(
                            border: Border(bottom: BorderSide(width: 1, color: Colors.black12)),
                          ),
                          padding: EdgeInsets.symmetric(vertical: 12,horizontal:10),
                          child: Row(
                            children: [
                              Expanded(
                                flex: 5,
                                child: Text(
                                  "${info.bname}",
                                  maxLines: 1,
                                  style: const TextStyle(
                                    overflow: TextOverflow.ellipsis,
                                    color: Colors.black87,
                                    fontSize: 18,
                                    fontWeight: FontWeight.normal,
                                  ),
                                ),
                              ),
                              Expanded(
                                flex: 3,
                                child: Text(
                                  "${abountTitle}",
                                  style: const TextStyle(
                                    fontWeight: FontWeight.w300,
                                    color: Colors.black87,
                                    fontSize: 14,
                                  ),
                                ),
                              ),
                              Expanded(
                                flex: 2,
                                child: Text(
                                  "${bcount}字",
                                  style: const TextStyle(
                                    fontWeight: FontWeight.w300,
                                    color: Colors.black87,
                                    fontSize: 14,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                        GestureDetector gesture = GestureDetector(
                          child: con,
                          onTap: (){
                            Log.i("info==>${info}");
                            //跳转
                            //跳转到阅读页
                            bookLibController.gotoBookDetail(info);
                          },
                        );
                        return gesture;
                      }else{
                        return const Padding(
                          padding: EdgeInsets.all(12.0),
                          child: Center(
                            child: SizedBox(
                              width: 18.0,
                              height: 18.0,
                              child: CircularProgressIndicator(
                                valueColor: AlwaysStoppedAnimation<Color>(
                                  Colors.grey,
                                ), // 颜色为灰色
                                strokeWidth: 1, // 线宽为3
                              ),
                            ),
                          ),
                        );
                      }
                    }
                )
            )
          ]
        );
      },
    ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}

// class BookLibPage extends StatelessWidget {
//   final RefreshController _refreshController = RefreshController(initialRefresh: false);
//
//    BookLibPage({Key? key}) : super(key: key);
//
//   @override
//   Widget build(BuildContext context) {
//     print("BookLibPage====>build===》");
//     String str = "我们的世界盗,台湾";
//     String bstr = ChineseHelper.convertToTraditionalChinese(str);
//     print("str=$str,bstr=$bstr");
//
//     return Container(
//       child: Center(
//         child: Text("BookLib",style: TextStyle(
//           color: Colors.yellow,
//           fontSize: 40,
//         ),),
//       ),
//     );
//   }
// }
